# -*- coding: utf-8 -*-

from datetime import datetime

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

from app.base.tables import tb
from app.config.configs import configs

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = "mysql+pymysql://"+configs['sqlalchemy']['db_user']+":"+configs['sqlalchemy']['db_pwd']+"@"+configs['sqlalchemy']['db_host']+":"+configs['sqlalchemy']['db_port']+"/"+configs['sqlalchemy']['db_name']
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
db = SQLAlchemy(app)


class Member(db.Model):
    __tablename__ = tb['common_member']
    member_id = db.Column(db.Integer, primary_key=True)
    pwd = db.Column(db.String(64))
    real_name = db.Column(db.String(32))
    email = db.Column(db.String(128), unique=True)
    phone = db.Column(db.String(11), unique=True)
    tel = db.Column(db.String(32))
    add_date = db.Column(db.DateTime, index=True, default=datetime.utcnow)
    add_member_id = db.Column(db.Integer)

    def __init__(self, pwd, real_name, email, phone, tel, add_date, add_member_id):
        self.pwd = pwd
        self.real_name = real_name
        self.email = email
        self.phone = phone
        self.tel = tel
        self.add_date = add_date
        self.add_member_id = add_member_id


class MemberLog(db.Model):
    __tablename__ = tb['common_member_log']
    member_log_id = db.Column(db.Integer, primary_key=True)
    member_id = db.Column(db.Integer, default=0)
    ip = db.Column(db.String(64))
    add_date = db.Column(db.DateTime, default=datetime.utcnow)

    def __init__(self, member_id, ip, add_date):
        self.member_id = member_id
        self.ip = ip
        self.add_date = add_date


if __name__ == "__main__":
    db.create_all()
    # 初始化一些数据
    db.session.add(Member('4297f44b13955235245b2497399d7a93', 'anti', '1@1.com', '13800001111', '0512', '2018-04-15 13:45:16', 0))
    db.session.commit()
